package verifyTool

import (
	"CommonsProject/pkg/tools/redisTool"
	"context"
	"errors"
	"github.com/wenlng/go-captcha/captcha"
)

// Verification 验证码验证
func Verification(uuid string, array [2][]int) {
	background := context.Background()
	value := redisTool.ClientRedis.Get(background, uuid)
	var pointInfo map[int]captcha.CharDot
	err := value.Scan(&pointInfo)
	if err != nil {
		panic(errors.New("系统错误：获取验证码失败！"))
	}
	if len(array) > len(pointInfo) || len(array) < len(pointInfo) {
		panic(errors.New("人机验证失败！"))
	}

	calculatePointInfo(pointInfo, array)
}

func calculatePointInfo(target map[int]captcha.CharDot, points [2][]int) {
	for key, value := range target {
		ints := points[key]
		x := ints[0]
		y := ints[1]

		if x < value.Dx && x > value.Dx+value.Width {
			panic(errors.New("人机验证失败！"))
		}

		if y < value.Dy && x > value.Dy+value.Height {
			panic(errors.New("人机验证失败！"))
		}
	}
}
